/**********************************************************************//**
            Meteor Firmware Platform
              
*-
@file   bubble.c
@author Lucas
@date   2021/04/27 13:35:01
@brief  bubble time: O(n^2)
**************************************************************************/
#include <stdio.h>
#include "config.h"


// arr ,acturally is a address,which point to the array data start pos
int bubble_sort(int arr[],int len,int is_ascsending)
{
    int i = 0;
    int j = 0;
    int tmp = 0;

    // total loop (n-1)(n-i-1)
    // loop (n-1)
    for( ;i < (len -1);i++)
    {
        j = i + 1;

	// loop (n-1-i)
         for( ;j < len;j++)
        {
            // this operate will improve the effecient when same element in array
            if(arr[i] == arr[j])
                continue;

            //  ascending or descending
            if(is_ascsending ? (arr[i] < arr[j]) : (arr[i] > arr[j])) 
            {
                tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
            }
        }
    }

    #if RESULT_PRINT
        // print
        printf("%s\r\n",is_ascsending ? "ascending" : "descending");
        for(i = 0;i< len;i++)
        {
            printf("%d,",arr[i]);
        }
        printf("\r\n");
    #endif

    // seems no need to reurn a value !
    return 0;
}
